package com.bestcem.xm.award.mq;//package com.bestcem.xm.award.util.mq;
//
//import com.alibaba.fastjson.JSON;
//import com.bestcem.xm.award.dao.AwardMqConsumeLogDao;
//import com.bestcem.xm.award.service.AwardMqHandler;
//import com.bestcem.xm.award.util.mq.dto.DrawAwardSendSmsDTO;
//import com.bestcem.xm.award.util.mq.dto.ProjectDeleteDTO;
//import com.bestcem.xm.config.RabbitMqConstantConfig;
//import com.bestcem.xm.util.MsgPackFormatUtil;
//import com.rabbitmq.client.Channel;
//import lombok.extern.slf4j.Slf4j;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.amqp.core.Message;
//import org.springframework.amqp.rabbit.annotation.RabbitHandler;
//import org.springframework.amqp.rabbit.annotation.RabbitListener;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.Resource;
//import java.util.Base64;
//
///**
// * 监听 奖励项目接收的mq消息
// *
// * @author sh.pei <sh.pei@idiaoyan.com>
// * @version v1.0
// * @date 2021/8/1 16:04
// */
//@Slf4j
//@Component
//public class AwardRabbitReceive {
//
//    @Autowired
//    private AwardMqHandler awardMqHandler;
//
//    @Resource
//    private AwardMqConsumeLogDao awardMqConsumeLogDao;
//
//    @Autowired
//    private RabbitMqConstantConfig rabbitMqConstantConfig;
//
//    private static final Logger EMAIL_LOG = LoggerFactory.getLogger("EmailErrorLog");
//
//    /**
//     * 监听 奖励mq消息
//     *
//     * @param dto
//     * @param channel
//     */
////    @RabbitListener(queues = "#{appAwardDelayQueue.name}", concurrency = "${app.award.rabbitmq.awardDelayQueue.concurrency:2}")
////    @RabbitHandler
////    public void handlerApp(Message dto, Channel channel) {
////        try {
////            if (log.isInfoEnabled()) {
////                log.info("奖励消费奖励mq消息收到消息:{}", Base64.getEncoder().encodeToString(dto.getBody()));
////            }
////            AwardMqConsumeLogDO consumeLog = new AwardMqConsumeLogDO();
////            consumeLog.setExchangeName(dto.getMessageProperties().getReceivedExchange());
////            consumeLog.setQueueName(rabbitMqConstantConfig.getAppBpAwardDelayQueue());
////            consumeLog.setRoutingKey(dto.getMessageProperties().getReceivedRoutingKey());
////            consumeLog.setOriginContent(Base64.getEncoder().encodeToString(dto.getBody()));
////            consumeLog.setStatus(MqConsumeStatusEnum.TO_CONSUME.getIndex());
////            if (dto.getMessageProperties().getHeaders().containsKey(AwardMqConstant.AWARD_MQ_HEADER)) {
////                Object headerId = dto.getMessageProperties().getHeaders().get(AwardMqConstant.AWARD_MQ_HEADER);
////                consumeLog.setSenderId(String.valueOf(headerId));
////            }
////            String id = awardMqConsumeLogDao.insert(consumeLog);
////            MsgPackFormatUtil.MqMessage mqMessage = MsgPackFormatUtil.mqDecode(dto.getBody());
////            if (mqMessage == null) {
////                log.error("奖励消费奖励mq消息解析失败:" + Base64.getEncoder().encodeToString(dto.getBody()));
////                if (EMAIL_LOG.isErrorEnabled()) {
////                    EMAIL_LOG.error("[award] mq消息消费解析失败,{}", Base64.getEncoder().encodeToString(dto.getBody()));
////                }
////                channel.basicNack(dto.getMessageProperties().getDeliveryTag(), false, false);
////
////                Integer updateCount = awardMqConsumeLogDao.updateStatusById(id, MqConsumeStatusEnum.FAIL_REQUEUE.getIndex());
////                if (updateCount != Constants.ONE) {
////                    log.error("[award] handlerApp 修改消息状态失败 id:{}", id);
////                }
////                return;
////            }
////            if (log.isInfoEnabled()) {
////                log.info("奖励消费奖励mq消息收到消息:{}", JSON.toJSONString(mqMessage));
////            }
////            Map map = mqMessage.getData();
////            boolean flag = false;
////            if (AwardMqConstant.DRAW_AWARD_START_TOPIC.equals(mqMessage.getTopic())) {
////                DrawAwardStartDTO drawAwardStartDTO = MsgPackFormatUtil.mapToObj(map, DrawAwardStartDTO.class);
////                flag = awardMqHandler.startDrawAward(drawAwardStartDTO);
////            } else if (AwardMqConstant.DRAW_AWARD_STOP_TOPIC.equals(mqMessage.getTopic())) {
////                DrawAwardStopDTO drawAwardStopDTO = MsgPackFormatUtil.mapToObj(map, DrawAwardStopDTO.class);
////                flag = awardMqHandler.stopDrawAward(drawAwardStopDTO);
////            } else if (AwardMqConstant.TEMP_CLEAN_TOPIC.equals(mqMessage.getTopic())) {
////                TempCleanDTO tempCleanDTO = MsgPackFormatUtil.mapToObj(map, TempCleanDTO.class);
////                flag = awardMqHandler.cleanAwardTempByTempId(tempCleanDTO);
////            } else if (AwardMqConstant.RED_PACKET_START_TOPIC.equals(mqMessage.getTopic())) {
////                RedPacketAwardStartDTO redPacketAwardStartDTO = MsgPackFormatUtil.mapToObj(map, RedPacketAwardStartDTO.class);
////                flag = awardMqHandler.startRedPacketAward(redPacketAwardStartDTO);
////            } else if (AwardMqConstant.RED_PACKET_STOP_TOPIC.equals(mqMessage.getTopic())) {
////                RedPacketAwardStopDTO redPacketAwardStopDTO = MsgPackFormatUtil.mapToObj(map, RedPacketAwardStopDTO.class);
////                flag = awardMqHandler.stopRedPacketAward(redPacketAwardStopDTO);
////            } else if (AwardMqConstant.RED_PACKET_SENDING_TOPIC.equals(mqMessage.getTopic())) {
////                RedPacketAwardSendingDTO redPacketAwardSendingDTO = MsgPackFormatUtil.mapToObj(map, RedPacketAwardSendingDTO.class);
////                flag = awardMqHandler.sendingRedPacketAward(redPacketAwardSendingDTO);
////            } else if (AwardMqConstant.AWARD_START_TOPIC.equals(mqMessage.getTopic())) {
////                AwardStartDTO awardStartDTO = MsgPackFormatUtil.mapToObj(map, AwardStartDTO.class);
////                awardMqHandler.startAward(awardStartDTO);
////            } else if (AwardMqConstant.AWARD_STOP_TOPIC.equals(mqMessage.getTopic())) {
////                AwardStopDTO awardStopDTO = MsgPackFormatUtil.mapToObj(map, AwardStopDTO.class);
////                awardMqHandler.stopAward(awardStopDTO);
////            } else {
////                log.error("消费奖励mq消息失败:" + Base64.getEncoder().encodeToString(dto.getBody()));
////                if (EMAIL_LOG.isErrorEnabled()) {
////                    EMAIL_LOG.error("[award] mq消息消费消费失败,{}", Base64.getEncoder().encodeToString(dto.getBody()));
////                }
////            }
////
////            channel.basicAck(dto.getMessageProperties().getDeliveryTag(), false);
////            int status = flag ? MqConsumeStatusEnum.SUCCESS.getIndex() : MqConsumeStatusEnum.FAIL_NO_QUEUE.getIndex();
////            Integer updateCount = awardMqConsumeLogDao.updateStatusById(id, status);
////            if (updateCount != Constants.ONE) {
////                log.error("[award] handlerApp 修改消息状态失败 id:{}", id);
////            }
////        } catch (Exception e) {
////            log.error("处理奖励app消息异常:" + Base64.getEncoder().encodeToString(dto.getBody()), e);
////            EMAIL_LOG.error("[award] mq消息消费消费异常{},{}", e.getMessage(), Base64.getEncoder().encodeToString(dto.getBody()));
////            try {
////                channel.basicNack(dto.getMessageProperties().getDeliveryTag(), false, false);
////            } catch (IOException ex) {
////                log.error("nack奖励消息异常", ex);
////            }
////        }
////    }
//
//    /**
//     * 监听 奖励mq项目删除消息
//     * @param dto
//     * @param channel
//     */
//    @RabbitListener(queues = "#{appAwardCommonQueue.name}", concurrency = "${app.award.rabbitmq.awardCommonQueue.concurrency:2}")
//    @RabbitHandler
//    public void handlerAwardCommon(Message dto, Channel channel) {
//        try {
//            MsgPackFormatUtil.MqMessage mqMessage = MsgPackFormatUtil.mqDecode(dto.getBody());
//            if (mqMessage == null) {
//                log.error("app消费mq消息解析失败:" + Base64.getEncoder().encodeToString(dto.getBody()));
//                channel.basicNack(dto.getMessageProperties().getDeliveryTag(), false, false);
//                return;
//            }
//            if (log.isInfoEnabled()) {
//                log.info("app消费mq消息收到消息:{}", JSON.toJSONString(mqMessage));
//            }
//
//            if (AwardMqConstant.PROJECT_DELETE_TOPIC.equals(mqMessage.getTopic())) {
//                ProjectDeleteDTO projectDeleteDTO = MsgPackFormatUtil.mapToObj(mqMessage.getData(), ProjectDeleteDTO.class);
//                awardMqHandler.deleteProject(projectDeleteDTO);
//            } else if (AwardMqConstant.DRAW_AWARD_SEND_SMS.equals(mqMessage.getTopic())) {
//                DrawAwardSendSmsDTO sendSmsDTO = MsgPackFormatUtil.mapToObj(mqMessage.getData(), DrawAwardSendSmsDTO.class);
//                awardMqHandler.sendDrawAwardSms(sendSmsDTO);
//            } else {
//                log.error("[award] 消费奖励mq消息失败:" + Base64.getEncoder().encodeToString(dto.getBody()));
//                if (EMAIL_LOG.isErrorEnabled()) {
//                    EMAIL_LOG.error("[award] mq消息消费消费失败,{}", Base64.getEncoder().encodeToString(dto.getBody()));
//                }
//            }
//            channel.basicAck(dto.getMessageProperties().getDeliveryTag(), false);
//        } catch (Exception e) {
//            EMAIL_LOG.error("[award] mq公共消息消费消费异常{},{}", e.getMessage(), Base64.getEncoder().encodeToString(dto.getBody()));
//            log.error("[award] mq公共消息消费消费异常:{}" + Base64.getEncoder().encodeToString(dto.getBody()), e);
//            try {
//                channel.basicNack(dto.getMessageProperties().getDeliveryTag(), false, false);
//            } catch (Exception e2) {
//                log.error("处理倍市得app的mq消息, nackmq消息异常", e2);
//            }
//        }
//    }
//}
